home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / xmmlrg.exe / XMMHNDLR.C < prev    next >
C/C++ Source or Header  |  1991-09-05  |  17KB  |  448 lines

  1. /*************************************************************************
  2. *                                                                        *
  3. *     XMMHandler - Access XMM for storing / accessing db_Vista records         *
  4. *                                                                                                  *
  5. *     This file implements the routines that the user calls. This library     *
  6. *     calls the modified XMM routines (for large model) as distributed by     *
  7. *     Microsoft.                                                              *
  8. *                                                                                                  *
  9. *     Author:        Tom Frank, BIMCO                                                         *
  10. *                                                                        *
  11. *************************************************************************/
  12.  
  13. #ifndef    LINT_ARGS
  14. #define    LINT_ARGS
  15. #endif
  16.  
  17. #ifndef    ANSI
  18. #define    ANSI
  19. #endif
  20.  
  21. #include    "xmm.h"
  22. #include "xmmhndlr.h"
  23. #include <malloc.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <memory.h>
  27. #include <string.h>
  28.  
  29. /*************************************************************************
  30. *                                                                        *
  31. *     Defines used to return error codes - these match the db_Vista             *
  32. *     conventions - here so we don't need vista.h                                     *
  33. *                                                                        *
  34. *************************************************************************/
  35.  
  36. #define    S_OKAY    0
  37. #define    S_NOTFOUND    2
  38.  
  39. /*************************************************************************
  40. *                                                                        *
  41. *     Defines used within this module                                                     *
  42. *                                                                        *
  43. *************************************************************************/
  44.  
  45. #define    TRUE    1
  46. #define    FALSE    0
  47. #define    UNUSED    -1
  48. #define    ONE_K        1024L
  49. #define    MAX_REC_LEN    256
  50.  
  51. /*************************************************************************
  52. *                                                                        *
  53. *     Data Structure Definitions that are not visible outside this module     *
  54. *                                                                        *
  55. *************************************************************************/
  56.  
  57. typedef struct _XMMHandle {                        /* XMM handle data structure */
  58.     short handle;                                        /* XMM handle */
  59.     int adj_rec_size;                                    /* user's rounded record size */
  60.     int act_rec_size;                                    /* user's actual record size */
  61.     long max_rec_alloc;                                /* highest rec # alloc */
  62.     long max_rec_used;                                /* highest rec # used */
  63.     } XMMHandle, *XMMHandlePTR;
  64.  
  65. /*************************************************************************
  66. *                                                                        *
  67. *     Global Data Available from this module                                             *
  68. *                                                                        *
  69. *************************************************************************/
  70.  
  71. int XMMErrorCode;                                        /* return code from XMM rtns */
  72.  
  73. /*************************************************************************
  74. *                                                                        *
  75. *     Local Data Storage - internal to this module                                     *
  76. *                                                                        *
  77. *************************************************************************/
  78.  
  79. static XMMHandlePTR XMM_Handle_Table;            /* pointer to handle table */
  80. static int initialized = FALSE;                    /* started?? */
  81. static int Max_Handle_Alloc;                        /* User requested max handles */
  82. static int Max_Handle_Used;                        /* last handle used */
  83. static char move_area[MAX_REC_LEN];                /* for data movement */
  84.  
  85. /*************************************************************************
  86. *                                                                        *
  87. *     Local Functions internal to this module                                         *
  88. *                                                                        *
  89. *************************************************************************/
  90.  
  91. static int null_record(char *buffer, int buff_len);
  92.  
  93. /*<f>*/
  94. int InitXMMSystem(int MaxHandles)
  95. /*************************************************************************
  96. *                                                                        *
  97. *     Initialize the XMM interface system - return TRUE / FALSE to signify     *
  98. *     our success level                                                                         *
  99. *                                                                        *
  100. *************************************************************************/
  101. {
  102.     int i;
  103.  
  104.     if (initialized)                                    /* already done?? */
  105.         return (S_NOTFOUND);                            /* yes */
  106.  
  107.     if (!XMM_Installed())                            /* is XMM handler around?? */
  108.         return (S_NOTFOUND);                            /* no */
  109.  
  110.     if ((XMM_Handle_Table=(XMMHandlePTR)malloc(sizeof(XMMHandle) * MaxHandles)) == NULL)
  111.         return (S_NOTFOUND);                            /* no memory */
  112.  
  113.     memset((char *)XMM_Handle_Table, '\0', sizeof(XMMHandle) * MaxHandles);
  114.  
  115.     for (i=0; i < MaxHandles; i++)                /* set all as unused */
  116.         XMM_Handle_Table[i].handle = UNUSED;
  117.  
  118.     initialized = TRUE;                                /* started now */
  119.     Max_Handle_Alloc = MaxHandles;                /* User requested max handles */
  120.     Max_Handle_Used = -1;                            /* last handle used (none yet) */
  121.  
  122.     return (S_OKAY);
  123.  
  124. }
  125.  
  126. /*<f>*/
  127. int ShutXMMSystem(void)
  128. /*************************************************************************
  129. *                                                                        *
  130. *     Close down the XMM interface system - return S_OKAY / S_NOTFOUND to      *
  131. *     signify our success level                                                             *
  132. *                                                                        *
  133. *************************************************************************/
  134. {
  135.     int i;
  136.  
  137.     if (!initialized)                                    /* already done?? */
  138.         return (S_NOTFOUND);                            /* yes */
  139.  
  140. /*************************************************************************
  141. *                                                                        *
  142. *     Insure all Handles have been released                                             *
  143. *                                                                        *
  144. *************************************************************************/
  145.  
  146.     for (i=0; i <= Max_Handle_Used; i++)
  147.         {
  148.         if (XMM_Handle_Table[i].handle != UNUSED)    /* in use?? */
  149.             XMM_FreeExtended(XMM_Handle_Table[i].handle);
  150.         }
  151.  
  152. /*************************************************************************
  153. *                                                                        *
  154. *     Drop memory for Handle Table                                                         *
  155. *                                                                        *
  156. *************************************************************************/
  157.  
  158.     free(XMM_Handle_Table);
  159.  
  160. /*************************************************************************
  161. *                                                                        *
  162. *     Reset Flags                                                                                 *
  163. *                                                                        *
  164. *************************************************************************/
  165.  
  166.     initialized = FALSE;                                /* not started now */
  167.  
  168.     return (S_OKAY);
  169.  
  170. }
  171.  
  172. /*<f>*/
  173. XHandle AllocateXMMArea(long MaxRecs, int RecSize)
  174. /*************************************************************************
  175. *                                                                        *
  176. *     Allocate an XMM area and return the handle to the user. A negative     *
  177. *     return is an error and the actual error (if -3) is in XMMErrorCode     *
  178. *                                                                        *
  179. *     Note: we allocate 1 more record than MaxRecs since 0 is a valid recno *
  180. *                                                                        *
  181. *************************************************************************/
  182. {
  183.     unsigned space_needed;
  184.     long ret_code;
  185.     XHandle hnd;
  186.     struct    XMM_Move CtrlBlk;
  187.     char block[ONE_K];
  188.     int i;
  189.     int adj_recsize;
  190.  
  191.     XMMErrorCode = 0;                                    /* clear code */
  192.  
  193.     if (!initialized)                                    /* started?? */
  194.         return (-1);                                    /* no */
  195.  
  196.     for (hnd=-1,i=0; i < Max